package com.jeesite.modules.yzw.service;

import com.jeesite.modules.yzw.constant.Constant;
import com.jeesite.modules.yzw.dao.OtsIflytekUserDao;
import com.jeesite.modules.yzw.entity.OtsIflytekUser;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Map;

/**
 * 讯飞用户同步Service
 * @author guohusong
 * @version 1.0
 * @date 2022/5/17 11:03
 */
@Service
public class OtsIfytekUserService {
    @Resource
    private OtsIflytekUserDao otsIflytekUserDao;

    public void modifyOtsIflytekUserStatus(Map map){
        otsIflytekUserDao.modifyOtsIflytekUserStatus(map);
    }

    public String queryLastIflytekUserSync(){
        return otsIflytekUserDao.queryLastIflytekUserSync();
    }

    /**
     * 新增或者修改用户信息
     * @param otsIflytekUser
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public int save(OtsIflytekUser otsIflytekUser) {
        otsIflytekUser.setCompid(Constant.IFLYTEK_COMPID);
        otsIflytekUser.setSyncupdatetime(new Date());
        //查询当前用户是否存在
        OtsIflytekUser oldOtsIflytekUser = getOtsIflytekUser(Constant.IFLYTEK_COMPID, otsIflytekUser.getId());
        if(oldOtsIflytekUser == null) {
            otsIflytekUser.setSynctime(new Date());
            otsIflytekUser.setSyncstate(Constant.Sync.SYNC_STATE_0);
            return otsIflytekUserDao.addOtsIflytekUser(otsIflytekUser);
        } else {
            otsIflytekUser.setSyncstate(Constant.Sync.SYNC_STATE_1);
            return otsIflytekUserDao.updateOtsIflytekUserByid(otsIflytekUser);
        }

    }

    /**
     * 根据id查询
     * @param compid
     * @param id
     * @return
     */
    public OtsIflytekUser getOtsIflytekUser(Long compid, String id) {
        OtsIflytekUser otsIflytekUser=new OtsIflytekUser();
        otsIflytekUser.setCompid(compid);
        otsIflytekUser.setId(id);
        return otsIflytekUserDao.getOtsIflytekUser(otsIflytekUser);
    }
}
